import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:online_study/mine/exam/exam_bean.dart';
import 'package:online_study/theme/colors.dart';
import 'package:online_study/theme/size.dart';

//错题分析的基本单元(一道题)
class AnswerItemPage extends StatefulWidget {
  ExamBean _bean;

  AnswerItemPage(this._bean);

  @override
  _AnswerItemPageState createState() => _AnswerItemPageState(_bean);
}

class _AnswerItemPageState extends State<AnswerItemPage> {
  ExamBean _bean;

  _AnswerItemPageState(this._bean);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: ThemeColor.colorWhite,
      padding: EdgeInsets.symmetric(vertical: ThemeSize.height12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [_buildQuestion(), _buildOptions(), _buildAnswers()],
      ),
    );
  }

  Widget _buildQuestion() {
    String type = "";
    if (_bean.isMulti) {
      type = "[多选]";
    } else {
      type = "[单选]";
    }
    String question = _bean.index.toString() + "." + type + _bean.question;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.max,
      children: [
        Container(
          padding: EdgeInsets.only(
              left: ThemeSize.width19,
              right: ThemeSize.width19,
              bottom: ThemeSize.height8),
          child: Text(question,
              style: TextStyle(
                  color: ThemeColor.color333333,
                  fontSize: ThemeSize.sp13,
                  fontWeight: FontWeight.bold),
              softWrap: true),
        ),
        _buildLabel()
      ],
    );
  }

  Widget _buildLabel() {
    var equal = listEquals(_bean.choiceAnswers, _bean.correctAnswers);
    if (equal) {
      return Container(
        alignment: Alignment.topRight,
        child: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(right: ThemeSize.width19),
          height: ThemeSize.height18,
          width: ThemeSize.width45,
          decoration: BoxDecoration(
              color: ThemeColor.color1485EE,
              borderRadius: BorderRadius.circular(ThemeSize.height8)),
          child: Text("正确",
              style: TextStyle(
                  color: ThemeColor.colorWhite, fontSize: ThemeSize.sp10)),
        ),
      );
    } else {
      return Container(
        alignment: Alignment.topRight,
        child: Container(
          alignment: Alignment.center,
          margin: EdgeInsets.only(right: ThemeSize.width19),
          height: ThemeSize.height18,
          width: ThemeSize.width45,
          decoration: BoxDecoration(
              color: ThemeColor.colorFA5151,
              borderRadius: BorderRadius.circular(ThemeSize.height8)),
          child: Text("错误",
              style: TextStyle(
                  color: ThemeColor.colorWhite, fontSize: ThemeSize.sp10)),
        ),
      );
    }
  }

  Widget _buildOptions() {
    return Column(
      children: _bean.answers
          .map((e) => Container(
        padding: EdgeInsets.only(
                    left: ThemeSize.width19,
                    right: ThemeSize.width19,
                    bottom: ThemeSize.height12),
                child: Container(
                  width: double.infinity,
                  child: Text(
                    e,
                    style: TextStyle(
                        color: ThemeColor.color333333,
                        fontSize: ThemeSize.sp13),
                  ),
                ),
              ))
          .toList(),
    );
  }

  Widget _buildAnswers() {
    return Row(
      children: [
        Container(
            margin: EdgeInsets.only(left: ThemeSize.width19),
            child: Text(
              "正确答案:" + _bean.correctAnswers.toString(),
              style: TextStyle(
                  color: ThemeColor.color555555, fontSize: ThemeSize.sp13),
            )),
        Container(
            margin: EdgeInsets.only(left: ThemeSize.width30),
            child: Text(
              "我的选项:" + _bean.choiceAnswers.toString(),
              style: TextStyle(
                  color: ThemeColor.color555555, fontSize: ThemeSize.sp13),
            ))
      ],
    );
  }
}
